package com.garen.sys.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Repository;

import com.garen.common.BaseDao;
import com.garen.sys.dao.IFireOrgDao;
import com.garen.sys.entity.FireOrg;
import com.garen.utils.MapBuilder;


@Repository
public class FireOrgDaoImpl extends BaseDao<FireOrg> implements IFireOrgDao {

	@PostConstruct
	private void init(){
		initOrm(FireOrg.class);
	}
	
	public List<FireOrg> getOrgListByType(int orgType){
		String sql = "select t.*,"
				+ " (select org_short_name from fire_org tt where tt.org_id = t.org_parent_id) as org_parent_name "
				+ " from firesys.fire_org t "
				+ " where t.org_type = 4 order by org_parent_id asc  ";		
		Map<String,Object> paramMap = new MapBuilder()
				.put("orgType", orgType)
				.build();
		return getListBySQL(sql, paramMap);
	}

	@Override
	public List<FireOrg> getFireOrgByAjbh(String ajbh) {
		// TODO Auto-generated method stub
		String sql = "select o.* from fire_org o,fire_case_info c where c.zgzd = o.org_uuid and ajbh = :ajbh";
		Map<String,Object> paramMap = new MapBuilder()
				.put("ajbh", ajbh)
				.build();
		return getListBySQL(sql, paramMap);
	}

	@Override
	public List<FireOrg> getAllParentOrgByOrgId(Integer orgId) {
		// TODO Auto-generated method stub
		List<FireOrg> resultList = new ArrayList<FireOrg>();
		FireOrg parent = getParentOrgByOrgId(orgId);
		while(parent!=null)
		{
			resultList.add(parent);
			parent = getParentOrgByOrgId(parent.getId());
		}
		
		
		return resultList;
	}

	@Override
	public FireOrg getParentOrgByOrgId(Integer orgId) {
		// TODO Auto-generated method stub
		String sql = "select * from fire_org where org_id = (select org_parent_id from fire_org where org_id = :orgId)";
		FireOrg result = null;
		
		Map<String,Object> paramMap = new MapBuilder()
				.put("orgId", orgId)
				.build();
		
		List<FireOrg> list = getListBySQL(sql, paramMap);
		if(list!=null&&list.size()>0)
		{
			result = list.get(0);
		}
		return result;
	}
	
}
